package com.sdhs.analyze.service;

import com.sdhs.analyze.bean.DicNodeType;
import com.sdhs.analyze.bean.DicThreshold;
import com.sdhs.analyze.dao.DicMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class DicService {

    @Autowired
    DicMapper dicMapper;

    private static List<DicThreshold> dicThresholdlist = null;

    private static Date lastRefreshTime = null;

    public Map<String, DicThreshold> getThreshold(){

        Map<String, DicThreshold> thresholdMap = new HashMap();

        List<DicThreshold> list = null;

        /* 计算五分钟前的时间 */
        Calendar calendar = Calendar.getInstance();

        calendar.setTime(new Date());

        calendar.add(Calendar.MINUTE, -5);

        if(dicThresholdlist != null && lastRefreshTime.after(calendar.getTime())) {

            list = dicThresholdlist;
        }else {

            lastRefreshTime = new Date();

            list = dicMapper.getDicThresholdList();

            dicThresholdlist = list;
        }
        if(list != null) {
            for (DicThreshold dic : list) {

                dic.setValueInt(Integer.parseInt(dic.getValue()));

                thresholdMap.put(dic.getName(), dic);
            }
        }

        return thresholdMap;
    }

    private static Date dicNodeTypeLastRefreshTime = null;

    private static Map<String, DicNodeType> nodeTypeMap = null;

    public Map<String, DicNodeType> getNodeType(){

        List<DicNodeType> list = null;

        /* 计算五分钟前的时间 */
        Calendar calendar = Calendar.getInstance();

        calendar.setTime(new Date());

        calendar.add(Calendar.MINUTE, -5);

        if(nodeTypeMap != null && dicNodeTypeLastRefreshTime.after(calendar.getTime())) {

            return nodeTypeMap;
        }else {

            nodeTypeMap = new HashMap<>();

            dicNodeTypeLastRefreshTime = new Date();

            list = dicMapper.getDicNodeTypeList();

            if(list != null) {
                for (DicNodeType dic : list) {

                    nodeTypeMap.put(dic.getNodeType(), dic);
                }
            }
            return nodeTypeMap;
        }
    }
}
